package yingke.com.yingke.ui.right;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.GridView;
import android.widget.ListView;

import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;
import yingke.com.yingke.R;
import yingke.com.yingke.config.Constant;
import yingke.com.yingke.ui.right.bean.LiveNodesBean;
import yingke.com.yingke.ui.right.bean.UserNodesBean;
import yingke.com.yingke.ui.right.dao.DownAndAnalysis;

public class SearchActivity extends AppCompatActivity implements View.OnClickListener {

    private ListView search_lv;
    private GridView gv_one,gv_two, gv_three;

    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            switch (msg.what){
                case 0:
                    JSONArray json = (JSONArray) msg.obj;
                    setGridAdapter(json);
                    break;
                case 3:
                    List<UserNodesBean.UsersBean> usersBeen = (List<UserNodesBean.UsersBean>) msg.obj;
                    setAdapter(usersBeen);
                    break;
            }
            return false;
        }
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_search);
        initView();
        initData();
    }
    private void setAdapter(List<UserNodesBean.UsersBean> usersBeen) {
        SearchAdapter adapter = new SearchAdapter(usersBeen,this);
        search_lv.setAdapter(adapter);
    }

    private void initData() {
        //填充listview
        DownAndAnalysis.getListData(Constant.SEARCH_URL,handler);
        //填充gridview
        DownAndAnalysis.getHeadData(Constant.SEARCH_URL,handler);
    }
    private void setGridAdapter(final JSONArray json){
        JSONArray mjson = json;
        final List<List<LiveNodesBean.LivesBean>> list = new ArrayList<>();
        Observable.create(new Observable.OnSubscribe<List<List<LiveNodesBean.LivesBean>>>() {
            @Override
            public void call(Subscriber<? super List<List<LiveNodesBean.LivesBean>>> subscriber) {
                try {
                    for (int i = 0; i < 3; i++) {
                        JSONObject jsonObject = json.getJSONObject(i);
                        Gson gson = new Gson();
                        LiveNodesBean lives = gson.fromJson(String.valueOf(jsonObject), LiveNodesBean.class);
                        List<LiveNodesBean.LivesBean> list_lives = lives.getLives();
                        list.add(list_lives);
                    }
                    subscriber.onNext(list);
                    subscriber.onCompleted();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }).subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<List<List<LiveNodesBean.LivesBean>>>() {
                    @Override
                    public void call(List<List<LiveNodesBean.LivesBean>> list) {
                            SearchHeadAdapter adapter_one = new SearchHeadAdapter(SearchActivity.this, list.get(0));
                            gv_one.setAdapter(adapter_one);
                            SearchHeadAdapter adapter_two = new SearchHeadAdapter(SearchActivity.this, list.get(1));
                            gv_two.setAdapter(adapter_two);
                            SearchHeadAdapter adapter_three = new SearchHeadAdapter(SearchActivity.this, list.get(2));
                            gv_three.setAdapter(adapter_three);
                    }
                });
    }
    private void initView() {
        search_lv = (ListView) findViewById(R.id.search_lv);
        View list_head = LayoutInflater.from(this).inflate(R.layout.search_head, null, false);
        gv_one = (GridView) list_head.findViewById(R.id.search_one_gv);
        gv_two = (GridView) list_head.findViewById(R.id.search_two_gv);
        gv_three = (GridView) list_head.findViewById(R.id.search_three_gv);
        findViewById(R.id.search_cancel_tv).setOnClickListener(this);
        search_lv.addHeaderView(list_head);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.search_cancel_tv:
                finish();
                break;
        }
    }
}
